import os

from celery_task.update_insects_task import recompute_insects_distribution
from insects_detection.project_utils import project_tools
from celery_task.classify_task import recompute_detailed_insects_info
from celery_task.imgprocess_task import process_originnal_imgs,process_sticky_card_imgs
from celery_task.chas_task import get_insects_chas
from celery.result import AsyncResult
import time

#获取工作目录路径
project_root_dir=project_tools.project_root_path()+os.sep+'insects_detection'
project_tools.prepare_working_dirs(project_root_dir)

gallery_path = project_root_dir+os.sep+project_tools.insects_img_dirname+os.sep+"Mar"
processed_gallery_path = project_root_dir + os.sep + project_tools.processed_img_dirname+os.sep+"may"
chas_data_path = project_root_dir + os.sep + project_tools.characteristic_dirname+os.sep+"mar"
model_path = project_root_dir + os.sep + project_tools.models_dirname + os.sep + project_tools.model_name
# print(chas_data_path)
# #分类计算温室病虫害信息异步调用
classify_task=recompute_detailed_insects_info.apply_async(args=[chas_data_path,model_path])
async_task_result=AsyncResult(classify_task.id)
print(async_task_result.get())
# print(classify_task.get())
# print(chas_data_path)

#原始图像预处理异步调用
# imgprocess_task=process_originnal_imgs.apply_async(args=[gallery_path,processed_gallery_path])
# async_task_result=AsyncResult(imgprocess_task.id)
# while True:
#     if async_task_result.info:
#         print(async_task_result.info)
#     if async_task_result.successful():
#         break
#     time.sleep(1)
# print(async_task_result.get())

#提取所有虫害特征数据异步调用
# chas_task=get_insects_chas.apply_async(args=[processed_gallery_path,chas_data_path])
# async_task_result=AsyncResult(chas_task.id)
# while True:
#     if async_task_result.info:
#         print(async_task_result.info)
#     if async_task_result.successful():
#         break
#     time.sleep(1)

#异步处理粘虫板图像，进行计数并返回base64格式图片
# count_task=process_sticky_card_imgs.apply_async(args=[processed_gallery_path+ os.sep + '1-3-3-A.jpg',False,None,True])
# count_task_result=AsyncResult(count_task.id)
# while True:
#     if count_task_result.info:
#         print(count_task_result.info)
#     if count_task_result.successful():
#         break
#     time.sleep(1)

# #一键更新虫情数据
# update_task=recompute_insects_distribution.apply_async(args=[gallery_path, processed_gallery_path, chas_data_path, model_path])
# update_task_result = AsyncResult(update_task.id)
# while True:
#     if update_task_result.info:
#         print(update_task_result.info)
#     if update_task_result.successful():
#         break
#     time.sleep(1)